package com.dz.scrm.view

import android.annotation.SuppressLint
import android.widget.ImageView
import android.widget.TextView
import com.dz.scrm.R
import com.dz.scrm.app.config.EVENT_DOWNLOAD_APK
import com.dz.scrm.mvp.model.entity.CheckVersionBean
import com.dz.scrm.mvp.model.entity.CommonMessageEntity
import com.dz.scrm.mvp.model.entity.EventMessage
import com.dz.scrm.mvp.presenter.DZPresent
import com.dz.scrm.mvp.ui.DZActivity
import com.dz.scrm.utils.DownLoadCallBack
import com.dz.scrm.utils.DownLoadHelper
import com.dz.scrm.utils.LoggerUtils
import com.dz.scrm.utils.showCheckVersionDialog
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView
import com.lxj.xpopup.core.CenterPopupView
import me.jessyan.art.integration.EventBusManager
import me.jessyan.art.mvp.Message
import me.jessyan.art.utils.ArtUtils
import me.jessyan.art.utils.DataHelper
import java.io.File

/**
 * 作者：fanda
 * 日期：2021/4/25 9:53
 * 描述：
 */
class UpdateVersionDialog(val activity: DZActivity<DZPresent>, private val checkVersionBean: CheckVersionBean) : CenterPopupView(activity) {

    private lateinit var mDownLoadLoading: BasePopupView

    override fun getImplLayoutId() = R.layout.dialog_update_version

    @SuppressLint("SetTextI18n")
    override fun onCreate() {
        super.onCreate()
        findViewById<TextView>(R.id.tv_version_name).text = "V${checkVersionBean.version}"
        findViewById<TextView>(R.id.tv_update).setOnClickListener {
            downloadApkFile(activity)
            dismiss()
        }
        findViewById<ImageView>(R.id.iv_close).setOnClickListener {
            dismiss()
            if (checkVersionBean.upgrade!!) {
                // 如果是强制升级，取消时直接关闭界面
                activity.finish()
            }
        }

    }

    private fun downloadApkFile(activity: DZActivity<DZPresent>) {
        // 如果还没有初始化
        if (!::mDownLoadLoading.isInitialized) {
            mDownLoadLoading = XPopup.Builder(activity).dismissOnBackPressed(false)
                    .isDestroyOnDismiss(false)
                    .dismissOnTouchOutside(false).asLoading("正在下载安装包...")
        }
        mDownLoadLoading.show()
        val apkDownloadPath = DataHelper.getCacheFilePath(activity) + File.separator + "apk" + File.separator + ArtUtils.encodeToMD5(checkVersionBean.address) + ".apk"
        DownLoadHelper.startSingleTask(checkVersionBean.address
                ?: "", apkDownloadPath, needBreakPoint = true, callBack = object : DownLoadCallBack {
            override fun onDownLoadReady(offset: Long, total: Long) {
                LoggerUtils.d("onDownLoadReady")
            }

            override fun onDownLoadComplete(savePath: String) {
                LoggerUtils.d("savePath: $savePath")
                mDownLoadLoading.smartDismiss()
                activity.checkInstallPermission(savePath)
            }

            override fun onDownLoadFail(state: String, errorMsg: String, speed: String) {
                LoggerUtils.d("onDownLoadFail: $errorMsg")
                mDownLoadLoading.smartDismiss()
            }

            override fun onDownLoading(offset: Long, total: Long, speed: String) {
                val progress = String.format("%.2f", offset * 100f / total) + "%"
                LoggerUtils.d("onDownLoading: $progress")
            }

        })
    }

}